package com.ymatou.shop.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.ymatou.shop.ui.view.CirclePageIndicator;
import com.ymatou.shop.ui.view.PageIndicator;
import com.ymatou.shop.R;
import com.ymatou.shop.model.Product;
import com.ymatou.shop.ui.view.ImageFragment;
import com.ymatou.shop.util.StringUtil;

public class ShowProductImageActivity extends FragmentActivity {
	
	public static final String EXTRA_URL_ARRAY = "extra_url_array";
	public static final String EXTRA_URL_CURRENT = "extra_url_current";

	ImageFragmentAdapter mAdapter;
	ViewPager mPager;
	PageIndicator mIndicator;
	private List<Product> mProductList;
	private int currentPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mProductList = new ArrayList<Product>();
		String[] images = getIntent().getStringArrayExtra(ShowProductImageActivity.EXTRA_URL_ARRAY);
		String currentUrl = getIntent().getStringExtra(EXTRA_URL_CURRENT);
		if (images != null && images.length > 0){
			for (int i=0;i<images.length;i++){
				mProductList.add(new Product(images[i]));
				if (StringUtil.isNotEmpty(currentUrl) && currentUrl.equals(images[i])){
					currentPosition = i;
				}
			}	
		}
		
		
		setContentView(R.layout.show_product_imageview);

		
		
		
		
		mAdapter = new ImageFragmentAdapter(getSupportFragmentManager());
		mAdapter.setCount(mProductList.size());
		mPager = (ViewPager) findViewById(R.id.pager);
		
		mPager.setAdapter(mAdapter);
		mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
		mIndicator.setViewPager(mPager);
		mPager.setCurrentItem(currentPosition);
	}
	
	public class ImageFragmentAdapter extends FragmentPagerAdapter {
    

    private int mCount;

    public ImageFragmentAdapter(FragmentManager fm) {
        super(fm);
    }
    
    @Override
    public Fragment getItem(int position) {
        return ImageFragment.newInstance(mProductList.get(position), getApplicationContext());
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
      // TODO Auto-generated method stub
    	if (position >= getCount()) {
				FragmentManager manager = ((Fragment) object).getFragmentManager();
				FragmentTransaction trans = manager.beginTransaction();
				trans.remove((Fragment) object);
				trans.commit();
			}
    }
    
    @Override
    public void finishUpdate(ViewGroup container) {
      // TODO Auto-generated method stub
      super.finishUpdate(container);
    }
    
    @Override
    public int getCount() {
        return mCount;
    }

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    }
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
	  // TODO Auto-generated method stub
//	  super.onSaveInstanceState(outState);
	}
}